package youleche.com.controler.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.log.LogFactory;

import youleche.com.business.BFixInfo;
import youleche.com.business.BLogInfo;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MFixInfo;
import youleche.com.model.MLogInfo;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SLogInfoServlet
 */
public class SLogInfoServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SLogInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0 :	
				//根据ID得到日志信息
				this.getLogById(request, response);
				break;	
			case 1 :	
				//得到日志信息
				this.getLogList(request, response);
				break;	
			case 2 :	
				//得到日志信息总条数
				this.getLogCount(request, response);
				break;	
		}
	}
	
	/**
	 * 根据ID得到日志信息
	 * @param request
	 * @param response
	 */
	private void getLogById(HttpServletRequest request, HttpServletResponse response){
		try{
			String logId = request.getParameter("id");
			BLogInfo handler = new BLogInfo();
			MLogInfo logInfo = handler.getLogInfoById(logId);
			JsonHelper.printObjectToJsonString(response, logInfo);
		}catch (Exception e) {
			MLLogFactory.exceptionLog("根据ID得到日志信息", "根据ID得到日志信息", e.getMessage(), "127.0.0.1");
			super.response(response, "");
		}
	}
	
	/**
	 * 得到日志信息
	 * @param request
	 * @param response
	 */
	private void getLogList(HttpServletRequest request, HttpServletResponse response){
		try{
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			BLogInfo handler = new BLogInfo();
			List<MLogInfo> list = handler.getLogInfoList(startIndex.toString(), ((Integer)pageSize).toString());
			JsonHelper.printListToJsonString(response, list);
		}catch (Exception e) {
			MLLogFactory.exceptionLog("得到日志信息", "得到日志信息", e.getMessage(), "127.0.0.1");
			super.response(response, "");
		}
	}
	
	/**
	 * 得到日志信息总条数
	 * @param request
	 * @param response
	 */
	private void getLogCount(HttpServletRequest request, HttpServletResponse response){
		try{
			
			BLogInfo handler = new BLogInfo();			
			super.response(response, handler.getLogCount());
		}catch (Exception e) {
			MLLogFactory.exceptionLog("得到日志信息总条数", "得到日志信息总条数", e.getMessage(), "127.0.0.1");
			super.response(response, "0");
		}
	}


}
